
//
//  UIButton+extension.swift
//  微博新浪
//
//  Created by hezerong on 16/6/4.
//  Copyright © 2016年 Rocky. All rights reserved.
//

import UIKit

extension UIButton {
 
    convenience init (titlt:String,titleColor:UIColor = UIColor.darkGrayColor(),font:CGFloat = 12,image:String? = nil,backGroundImage:String? = nil,target:AnyObject? = nil , action:String?=nil,event:UIControlEvents = .TouchUpInside){

        self.init()
        
        self.setTitle(titlt, forState: .Normal)
        self.setTitleColor(titleColor, forState: .Normal)
        self.titleLabel?.font = UIFont.systemFontOfSize(font)
        
        if let image = image{
        
           self.setImage(UIImage(named: image), forState: .Normal)
           self.setImage(UIImage(named:"\(image)_highlighted"), forState: .Highlighted)
            
        }
        
        if let backGroundImage = backGroundImage{
        
           self.setBackgroundImage(UIImage(named: backGroundImage), forState: .Normal)
           self.setBackgroundImage(UIImage(named: "\(backGroundImage)_highlighted"), forState: .Highlighted)
        }

        //action如果直接写，可以用字符串，如果做为参数传递，则需要使用selector
        let selector = Selector(action!)
        self.addTarget(target, action: selector, forControlEvents: event)

    }
}